package genedit.controller.interfaceActions;

import genedit.controller.ConnectState;
import genedit.controller.LinkCreator;
import genedit.model.Workspace;
import genedit.view.graph.GraphDocumentView;
import genedit.view.graph.GraphWorkArea;

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;

/**
 * Akcija za dodavanje veze između dva elementa.
 * 
 * @author Igić
 * @version 1.1 10/12/13
 */
@SuppressWarnings("serial")
public class GPolylineA extends AbstractAction {

	protected static GPolylineA instance = null;

	public GPolylineA() {
		putValue(NAME, "");
		putValue(SMALL_ICON, new ImageIcon("resources/icons/layer_select_point.png"));
		putValue(MNEMONIC_KEY, KeyEvent.VK_M);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK));
		putValue(SHORT_DESCRIPTION, "Polyline (Ctrl+M)");
	}

	public static AbstractAction getInstance() {
		if (instance == null)
			instance = new GPolylineA();
		return instance;
	}

	/**
	 * Akcija postavlja globalno aktivno stanje na stanje linkovanja, uz creator
	 * obične polyline veze.
	 * 
	 * @see LinkCreator, Link
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		Workspace.getInstance().setActiveState(new ConnectState(null, new LinkCreator()));

		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Image image = toolkit.getImage("resources/icons/cursorAdd1.png");
		Point hotSpot = new Point(0, 0);
		Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "CursorProba");

		GraphDocumentView document = Workspace.getInstance().getActiveDocument();
		if (document != null) {
			GraphWorkArea test = ((GraphWorkArea) document.getWorkArea());

			test.setCursor(cursor);
			((GraphWorkArea) document.getWorkArea()).getController().setCurrentState(
					new ConnectState((GraphWorkArea) Workspace.getInstance().getActiveDocument()
							.getWorkArea(), new LinkCreator()));
		}
	}

}
